<?php
/**
 * View  LaFabbrica.org
 * 
 * @author     luigi.pastore (gigi@la-fabbrica.org)
 * @created    17-mag-2011
 * @package    LaFabbrica
 * @copyright  Copyright (c) 2011 Luigi Pastore
 */
?>
<script type="text/javascript" src="/js/ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="/js/ckeditor/adapters/jquery.js"></script>
<script type="text/javascript" src="/js/ckeditor/load-editor.js"></script>
<script type="text/javascript">
    //<![CDATA[
     
    $(document).ready(function() {
          
	// Datepicker
	$( "#activity_start_date_y" ).datepicker({
	    // Riempie i campi della data...
	    onSelect: function(dateText, inst) {
		var theDate = new Date(Date.parse($(this).datepicker('getDate')));
		var day = $.datepicker.formatDate('dd', theDate);
		var month = $.datepicker.formatDate('mm', theDate);
		var year = $.datepicker.formatDate('yy', theDate);
		$('#activity_start_date_d').val(day);
		$('#activity_start_date_m').val(month);
		$('#activity_start_date_y').val(year);
	    },
	    showOn: "button",
	    showAnim: 'blind',
	    buttonImage: "/images/admin/calendar.png",
	    buttonImageOnly: true,
            defaultDate: '<?php echo $this->data['activity_start_date_m'] ? $this->data['activity_start_date_m'] : date( 'm' ) ?>/<?php echo $this->data['activity_start_date_d'] ? $this->data['activity_start_date_d'] : date( 'd' ) ?>/<?php echo $this->data['activity_start_date_y'] ? $this->data['activity_start_date_y'] : date( 'Y' ) ?>'
	});
        
	$( "#activity_end_date_y" ).datepicker({
	    // Riempie i campi della data...
	    onSelect: function(dateText, inst) {
		var theDate = new Date(Date.parse($(this).datepicker('getDate')));
		var day = $.datepicker.formatDate('dd', theDate);
		var month = $.datepicker.formatDate('mm', theDate);
		var year = $.datepicker.formatDate('yy', theDate);
		$('#activity_end_date_d').val(day);
		$('#activity_end_date_m').val(month);
		$('#activity_end_date_y').val(year);
	    },
	    showOn: "button",
	    showAnim: 'blind',
	    buttonImage: "/images/admin/calendar.png",
	    buttonImageOnly: true,
            defaultDate: '<?php echo $this->data['activity_end_date_m'] ? $this->data['activity_end_date_m'] : date( 'm' ) ?>/<?php echo $this->data['activity_end_date_d'] ? $this->data['activity_end_date_d'] : date( 'd' ) ?>/<?php echo $this->data['activity_end_date_y'] ? $this->data['activity_end_date_y'] : date( 'Y' ) ?>'
	});
        
        validator.groups['activity_start_date_d'] = 'startDate';
        validator.groups['activity_start_date_m'] = 'startDate';
        validator.groups['activity_start_date_y'] = 'startDate';
        validator.groups['activity_start_time_h'] = 'startTime';
        validator.groups['activity_start_time_m'] = 'startTime';
        validator.groups['activity_end_date_d'] = 'endDate';
        validator.groups['activity_end_date_m'] = 'endDate';
        validator.groups['activity_end_date_y'] = 'endDate';
        validator.groups['activity_end_time_h'] = 'endTime';
        validator.groups['activity_end_time_m'] = 'endTime';
        
//        validator.groups = {day: "dateValid", month: "dateValid", year: "dateValid",
//                            day_opt: "dateValidOptional", month_opt: "dateValidOptional", year_opt: "dateValidOptional"}
                        
        $("#activity_title").rules("add", {
            required: true,
            messages: {
                required: "<?php echo ERR_EMPTY ?>"
            }
        });
                        
        $("#activity_start_date_d").rules("add", {
            required: function(element) {
                return ($("#activity_end_date_d").val() == '' && $("#activity_end_date_m").val() == '' && $("#activity_end_date_y").val() == '')
                ||
                ($("#activity_start_time_h").val() != '' || $("#activity_start_time_m").val() != '');
            },
            messages: {
                required: "<?php echo ERR_EMPTY ?>"
            }
        });

        $("#activity_start_date_m").rules("add", {
            required: function(element) {
                return ($("#activity_end_date_d").val() == '' && $("#activity_end_date_m").val() == '' && $("#activity_end_date_y").val() == '')
                ||
                ($("#activity_start_time_h").val() != '' || $("#activity_start_time_m").val() != '');
            },
            messages: {
                required: "<?php echo ERR_EMPTY ?>"
            }
        });
                        
        $("#activity_start_date_y").rules("add", {
            required: function(element) {
                return ($("#activity_end_date_d").val() == '' && $("#activity_end_date_m").val() == '' && $("#activity_end_date_y").val() == '')
                ||
                ($("#activity_start_time_h").val() != '' || $("#activity_start_time_m").val() != '');
            },
            messages: {
                required: "<?php echo ERR_EMPTY ?>"
            }
        });
        
        $("#activity_start_time_h").rules("add", {
            required: "#activity_start_time_m:filled",        
            number: true,
            range: [0, 24],
            messages: {
                required: "<?php echo sprintf( ERR_EMPTY_FIELD, 'Ora' ) ?>",
                number: "<?php echo sprintf( ERR_FORMAT, 'Ora Dal' ) ?>",
                range: "<?php echo sprintf( ERR_FORMAT, 'Ora Dal' ) ?>"
            }
        });
        
        $("#activity_start_time_m").rules("add", {
            required: "#activity_start_time_h:filled",    
            number: true,
            range: [0, 59],
            messages: {
                required: "<?php echo sprintf( ERR_EMPTY_FIELD, 'Minuti' ) ?>",
                number: "<?php echo sprintf( ERR_FORMAT, 'Ora Dal' ) ?>",
                range: "<?php echo sprintf( ERR_FORMAT, 'Ora Dal' ) ?>"
            }
        });     
        
        $("#activity_end_time_h").rules("add", {
            required: "#activity_end_time_m:filled",        
            number: true,
            range: [0, 24],
            messages: {
                required: "<?php echo sprintf( ERR_EMPTY_FIELD, 'Ora' ) ?>",
                number: "<?php echo sprintf( ERR_FORMAT, 'Ora Al' ) ?>",
                range: "<?php echo sprintf( ERR_FORMAT, 'Ora Al' ) ?>"
            }
        });
        
        $("#activity_end_time_m").rules("add", {
            required: "#activity_end_time_h:filled",         
            number: true,
            range: [0, 59],
            messages: {
                required: "<?php echo sprintf( ERR_EMPTY_FIELD, 'Minuti' ) ?>",
                number: "<?php echo sprintf( ERR_FORMAT, 'Ora Al' ) ?>",
                range: "<?php echo sprintf( ERR_FORMAT, 'Ora Al' ) ?>"
            }
        });     
        
        $( '#activity_description' ).ckeditor(function(){}, ckeditorConf );
        
        // Risolve un bug d'integrazione tra jQuery e CKeditor
	$("#salva").click(function(){
            CKEDITOR.instances.activity_description.updateElement();

            validator.form();
            return validator.element( "#activity_description" );
	});
    });
	

    //]]>
</script>
<div id="content">
    <div id="summary" class="activities">
	<div class="head activities">
            <h2>Attivit&agrave;:</h2>
	</div>
	<div class="body">

	</div>	
    </div>
<form id="form-live" class="fab" method="post" action="/admin/attivita/info/id/<?php echo $this->activity_id ?>">

	<ul>
	    <li id="errorLi" style="<?php echo $this->errorMsg ? '' : 'display:none' ?>">
		<h3 id="errorMsgLbl">Si &egrave; verificato un problema durante l'invio del modulo.</h3>
		<p id="errorMsg">
                    Di seguito trovi evidenziati gli errori riscontrati.<?php $this->errorMsg ? var_dump( $this->errorMsg ) : '' ?>
		</p>
	    </li>
            
	    <li class="section">
                <h3>Cosa?</h3>
                <div id="instruct3">Inserisci la descrizione dell'attivit&agrave;.</div>
	    </li>            
            <li class="<?php echo $this->errorMsg['activity_title'] ? 'error' : '' ?>">
                <label class="desc <?php echo $this->errorMsg['activity_title'] ? 'error' : '' ?>">Titolo<span class="req">*</span></label>
                <div>
                    <span>
                        <input id="activity_title" name="activity_title" class="field text large" type="text" maxlength="255" value="<?php echo $this->data['activity_title'] ?>" /> 
                    </span>
                </div>
                <p class="error" style="<?php echo $this->errorMsg['activity_title'] ? 'display:block' : '' ?>"><?php echo $this->errorMsg['activity_title'] ?></p>
            </li>
	    <li class="<?php echo $this->errorMsg['activity_description'] ? 'error' : '' ?>">
		<label class="desc <?php echo $this->errorMsg['activity_description'] ? 'error' : '' ?>">Descrizione</label>
		<div>
		    <span>
			<textarea id="activity_description" class="field textarea small" name="activity_description" rows="3" cols="100"><?php echo htmlentities( $this->data['activity_description'] ) ?></textarea>
		    </span>
		</div>
		<p class="error" style="<?php echo $this->errorMsg['activity_description'] ? 'display:block' : '' ?>"><?php echo $this->errorMsg['activity_description'] ?></p>
	    </li>   

	    <li class="section">
                <h3>Quando?</h3>
                <div id="instruct3">Inserisci la data di partenza ed eventualmente la data di scadenza.</div>
	    </li>

	    <li class="<?php echo $this->errorMsg['activity_start_date'] ? 'error' : '' ?>">
		<label class="desc <?php echo $this->errorMsg['activity_start_date'] ? 'error' : '' ?>">Dal<span class="req">*</span></label>
		<div class="float-left">
		    <span class="input">
			<input class="field text dateValidOptional" name="activity_start_date_d" id="activity_start_date_d" size="2" type="text" maxlength="2" value="<?php echo $this->data['activity_start_date_d'] ? $this->data['activity_start_date_d'] : '' ?>" /> / 
			<label>GG</label>
		    </span>

		    <span class="input">
			<input class="field text dateValidOptional" name="activity_start_date_m" id="activity_start_date_m" size="2" type="text" maxlength="2" value="<?php echo $this->data['activity_start_date_m'] ? $this->data['activity_start_date_m'] : '' ?>" /> / 
			<label>MM</label>
		    </span>

		    <span class="input">
			<input class="field text dateValidOptional" name="activity_start_date_y" id="activity_start_date_y" size="4" type="text" maxlength="4" value="<?php echo $this->data['activity_start_date_y'] ? $this->data['activity_start_date_y'] : '' ?>" />
			alle<label>AAAA</label>
		    </span>
                    
		    <span class="input">
			<input class="field text hour" size="2" name="activity_start_time_h" id="activity_start_time_h" type="text" maxlength="2" value="<?php echo $this->data['activity_start_time_h'] ? $this->data['activity_start_time_h'] : '' ?>"/> : 
			<label for="activity_start_time_h">HH</label>
		    </span>

		    <span class="input">
			<input class="field text" size="2" name="activity_start_time_m" id="activity_start_time_m" type="text" maxlength="2" value="<?php echo $this->data['activity_start_time_m'] ? $this->data['activity_start_time_m'] : '' ?>"/>
			<label for="activity_start_time_m">MM</label>
		    </span>
                    
		<p class="error" style="<?php echo $this->errorMsg['activity_start_date'] ? 'display:block' : '' ?>"><?php echo $this->errorMsg['activity_start_date'] ?></p>
		</div>
                <p class="instruct">
                    <small>Inserisci le date di validit&agrave;:<br/>
                        Per attivit&agrave; giornaliere inserisci solo la data d'inizio.
                    </small>
                </p>
	    </li>
	    <li class="<?php echo $this->errorMsg['activity_end_date'] ? 'error' : '' ?>">
		<label class="desc <?php echo $this->errorMsg['activity_end_date'] ? 'error' : '' ?>">Scadenza</label>
		<div class="float-left">
		    <span class="input">
			<input class="field text dateValidOptional" name="activity_end_date_d" id="activity_end_date_d" size="2" type="text" maxlength="2" value="<?php echo $this->data['activity_end_date_d'] ? $this->data['activity_end_date_d'] : '' ?>" /> / 
			<label>GG</label>
		    </span>

		    <span class="input">
			<input class="field text dateValidOptional" name="activity_end_date_m" id="activity_end_date_m" size="2" type="text" maxlength="2" value="<?php echo $this->data['activity_end_date_m'] ? $this->data['activity_end_date_m'] : '' ?>" /> / 
			<label>MM</label>
		    </span>

		    <span class="input">
			<input class="field text dateValidOptional" name="activity_end_date_y" id="activity_end_date_y" size="4" type="text" maxlength="4" value="<?php echo $this->data['activity_end_date_y'] ? $this->data['activity_end_date_y'] : '' ?>" />
			alle<label>AAAA</label>
		    </span>
                    
		    <span class="input">
			<input class="field text hour" size="2" name="activity_end_time_h" id="activity_end_time_h" type="text" maxlength="2" value="<?php echo $this->data['activity_end_time_h'] ? $this->data['activity_end_time_h'] : '' ?>"/> : 
			<label for="activity_end_time_h">HH</label>
		    </span>

		    <span class="input">
			<input class="field text" size="2" name="activity_end_time_m" id="activity_end_time_m" type="text" maxlength="2" value="<?php echo $this->data['activity_end_time_m'] ? $this->data['activity_end_time_m'] : '' ?>"/>
			<label for="activity_end_time_m">MM</label>
		    </span>
                    
		<p class="error" style="<?php echo $this->errorMsg['activity_end_date'] ? 'display:block' : '' ?>"><?php echo $this->errorMsg['activity_end_date'] ?></p>
		</div>
	    </li>
            
            <li class="section">
                <h3>Chi?</h3>
                <div id="instruct3">Seleziona l'utente a cui vuoi assegnare l'attivit&agrave; (opzionale).</div>
	    </li>
            
            <li class="<?php echo $this->errorMsg['activity_assignedto'] ? 'error' : '' ?>">
                <label class="desc <?php echo $this->errorMsg['activity_assignedto'] ? 'error' : '' ?>">Assegna a:</label>
                <div>
                    <select class="field select medium" name="activity_assignedto" id="activity_assignedto">
                        <option value=""></option>
                        <?php foreach( $this->users AS $k => $user ): ?>
                        <option value="<?php echo $k?>" <?php echo $this->data['activity_assignedto'] == $k ? 'selected="selected"' : '' ?> ><?php echo $user['user_firstname']?> <?php echo $user['user_lastname']?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <p class="error" style="<?php echo $this->errorMsg['post_category'] ? 'display:block' : '' ?>"><?php echo $this->errorMsg['post_category'] ?></p>
            </li>         
	        
	</ul>
	<br style="clear: both" />
</div>

<input type="hidden" name="conferma" id="conferma" value="salva" />
<div id="formButtons">
    <button id="salva" name="salva" value="salva" class="button positive">
	<img src="/images/admin/tick.png" alt="" />Continua
    </button>
    <button id="annulla" name="annulla" value="annulla" class="button negative">
	<img src="/images/admin/cross.png" alt="" />Annulla
    </button>
</form>

</div>
